
import glob
import pepe.p_filesystem.dfstats as dfs
from pepe.p_txt.txt_report_or_flat import main as txtrf

def main(f):
    """Guess how many rows there is in flat delimited file
    """
    if txtrf(f) == 'guessed as txt flat':
        fh = open(f, 'r')

        file_info = dfs.dfstats(f, options=[])
    
        buf = ''

        cnt = 1
        while cnt < 10000:
            cnt += 1
            buf += fh.readline()

        fh.close()

        ls_avg = int(len(buf)/cnt)
        if ls_avg <> 0:
            e = str(int(file_info['size']/ls_avg))
        else:
            e = 0
        return 'estimated rows count (' + str(e) + ')'
    else:
        return 'guessed as txt report'

if 0: # test
    
    #for f in glob.glob('*.txt'):
    #for f in glob.glob(r'A:\Armada\2010-09-20\E8MARM4001R\*.pcl'):
    #for f in glob.glob(r'A:\EIFFEL_IC\2010-12-02\Zipped_Eiffel_IC_Bkgs_Entity_18_200710\*.txt'):
    for f in glob.glob(r'A:\SAP SAIL\2010-09-01\original\CPO_CSKS_DATA.txt'):
    
        print f, main(f)
    
